package com.android.bookgarden.tabfragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.bookgarden.MainActivity;
import com.android.bookgarden.R;
import com.android.bookgarden.adapter.TeacherAdapter;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseFragment;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.HomeDataBean;
import com.android.bookgarden.bean.TeacherBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;
import com.lzy.okgo.model.HttpHeaders;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 教师列表
 * Create by zouqh
 * on 2018/12/4
 **/
public class JiaoshiFragment extends BaseFragment implements RealtConstract.View {
    @BindView(R.id.recycler)
    RecyclerView recycler;

    private TeacherAdapter adapter;
    private List<TeacherBean> list;

    private RealtConstract.Presenter presenter;
    private int pageIndex = 1;
    private int pageSize = 40;
    private String id;


    public static JiaoshiFragment newInstance( String id) {
        JiaoshiFragment fragment = new JiaoshiFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("mer_id", "" + id);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    protected int getLayoutId() {
        return R.layout.jiaoshi_frament;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null!=getArguments()){
            id = (String) getArguments().getSerializable("mer_id");
        }
    }

    @Override
    protected void initView() {
        presenter = new BasePersenter(this,getActivity());
        list = new ArrayList<>();

        adapter = new TeacherAdapter(list, getActivity());
        recycler.setAdapter(adapter);
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        getList();

    }

    private void getList() {


        presenter.loadDataHeader(null, "", Apis.TEACHER_LIST +pageIndex+"/"+pageSize+"/"+id, 1001, Utlis.getHeadler(getActivity()), HttpType.GET);
    }


    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }

        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {
                case 1001:
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        List<TeacherBean> listD = JsonUitl.stringToList(bean.getData(), TeacherBean.class);
                        if (null == listD || listD.size() <= 0) {
                            return;
                        }
                        list.addAll(listD);
                        adapter.notifyDataSetChanged();

                    } else {
                        showToast(bean.getMessage() + "");
                    }

                    break;



            }
        }
    };
}
